package com.ruitu.health_1.mvp.presenter;

import com.ruitu.arad.base.BasePresenter;
import com.ruitu.health_1.mvp.model.CommentModel;
import com.ruitu.health_1.ui.CommentActivity;
import com.ruitu.router_module.bean.BaseResult2;

import java.io.File;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CommentPresenter {

    public static class CommentP extends BasePresenter<CommentActivity, CommentModel.CommentM> {
        public MultipartBody multiUpload(List<File> file) {
            MediaType FORM_CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
            MultipartBody.Builder builder = new MultipartBody.Builder();
            if (file !=null){
                for (int i = 0; i < file.size(); i++) {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file.get(i));
                    builder.addFormDataPart("files", file.get(i).getName(), requestBody);
                }
            }

            MultipartBody multipartBody = builder.build();
            return multipartBody;
        }
        public Disposable multiUpload(MultipartBody body){
            return m.multiUpload(body)
                    .subscribe(new Consumer<BaseResult2<String>>() {
                        @Override
                        public void accept(BaseResult2<String> baseResult2) throws Exception {

                            if (baseResult2.isSuccess()){
                                v.picUrl(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }

        public Disposable comment(RequestBody body) {
            return m.comment(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.commentSuc();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }


    }
}
